import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/a_yyt/models/medical_elder_entity.dart';
import 'package:ls_work/model/medicine_advice_model.dart';
import 'package:ls_work/net/response/medicine_advice/drug_list_data.dart';
import 'package:ls_work/ui/medicine/choose_medicine_elder_page.dart';
import 'package:ls_work/ui/select_old_page.dart';
import 'package:ls_work/util/ext_utils.dart';

import '../../../net/response/medicine_advice/medicine_elder_list_data.dart';
import '../../../ui/medicine/choose_medicine_page.dart';
class AddReceiveMedicineViewModel extends BaseViewModel{
  MedicineElderListData? elderInfo;

  AddReceiveMedicineViewModel(super.context);

  ///包装数量
  String? packageNum;
  ///最小单位数量
  String? unitNum;

  DrugListData? drugInfo;

  var model = MedicineAdviceModel();

  Future<void> chooseElder() async {

    var old =  await chooseMedicineElder(context);
    if(old!=null){
      elderInfo = old;
      notifyListeners();
    }
  }

  String totalNum = "";

  Future<void> chooseMedicine() async {
   DrugListData? data = await Go().push(ChooseMedicinePage());
   if(data!=null){
     drugInfo = data;
     notifyListeners();
   }
  }

  Future<void> calculateReceiveNum() async {
    if(drugInfo==null){
      return;
    }
    try{
      var number = await model.calculateMinNum(drugId: drugInfo!.id, maxNum: packageNum, minNum: unitNum);
      totalNum = number.toString();
      notifyListeners();
    }catch(e){
      showToast(e.toNetError());
    }
  }

  void addClick(){
    if(elderInfo==null){
      showToast("请选择长者");
      return;
    }
    if(drugInfo==null){
      showToast("请选择药品");
      return;
    }
    showApplyDialog(context: context, onSubmit: (){
      submitAdd();
    },message: '确定要提交吗？');
  }

  Future<void> submitAdd() async {
    showLoadingDialog(msg: '提交中...');
    try{
      await model.submitReceiveMedicine(customerId: elderInfo!.customerId,stockId: elderInfo!.id,drugId: drugInfo!.id,quantity: totalNum);
      hideDialog();
      Go().pop(result: true);
    }catch(e){
      hideDialog();
      showToast(e.toNetError());
    }
  }

}